//
//  CyclePageViewDemoTestController.m
//  HyCycleView
//  https://github.com/hydreamit/HyCycleView
//
//  Created by Hy on 2016/5/20.
//  Copyright © 2016年 Hy. All rights reserved.
//

#import "QuDanHomeRecommendController.h"
#import "QuDanHomeViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "QuDanHomeDetailController.h"
#import "AppDelegate.h"
#import "QuDanHomeModel.h"
#import "QuDanSquareNetWork.h"
@interface QuDanHomeRecommendController ()
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,strong)QuDanHomeModel *homeModel;
@property (nonatomic,strong)NSMutableArray *recommendListArr;
@property (nonatomic,assign)int currentPage;
@end
@implementation QuDanHomeRecommendController
- (NSMutableArray *)recommendListArr
{
    if (!_recommendListArr) {
        _recommendListArr = [NSMutableArray array];
    }
    return _recommendListArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableView.sectionHeaderHeight = 0.01;
    self.tableView.sectionFooterHeight = 0.01;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"QuDanHomeViewCell" bundle:nil] forCellReuseIdentifier:@"QuDanHomeViewCell"];
    [self loadRecommendNewData];
}
-(void)loadRecommendNewData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"lat"] = @"";
    param[@"lng"] = @"";
    param[@"pageNo"] = @"1";
    param[@"pageSize"] = @"10";
    param[@"type"] = @"1";
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@",QD_BaseURL,@"/qw/app/orderList"];
    [[QuDanNetworkManger sharedNetworkManger]POST:baseUrl parameters:param success:^(QuDanResponseModel * _Nonnull response) {
        NSLog(@"orderList.推荐:%@",response.data);
        self.homeModel = [QuDanHomeModel mj_objectWithKeyValues:response.data];
        [self.tableView reloadData];
    }failure:^(NSError * _Nonnull error) {
    }];
}
#pragma mark - QuDanHomeRecommendController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.homeModel.records.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QuDanHomeViewCell *homeCell = [tableView dequeueReusableCellWithIdentifier:@"QuDanHomeViewCell" forIndexPath:indexPath];
    homeCell.selectionStyle = UITableViewCellSelectionStyleNone;
    QuDanHomeRecordsItem *item = self.homeModel.records[indexPath.row];
    homeCell.itemModel = item;
    return homeCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuDanHomeRecordsItem *item = self.homeModel.records[indexPath.row];
    if (item.imgs.count == 0) {
        return 115 + 10;;
    }else{
        return  115 + (QD_IPHONE_WIDTH -20)/3+10;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QuDanHomeRecordsItem *itemModel = self.homeModel.records[indexPath.row];
    UITabBarController *tab = (UITabBarController *)kWindow.rootViewController;
    UINavigationController *nav = tab.viewControllers[tab.selectedIndex];
    QuDanHomeDetailController *homedetailvc = [[QuDanHomeDetailController alloc]init];
    homedetailvc.hidesBottomBarWhenPushed = YES;
    homedetailvc.publishOrderId = itemModel.publishOrderId;
    [nav pushViewController:homedetailvc animated:YES];
}
#pragma mark --  HyCyclePageViewProviderProtocol
- (void)configCyclePageView:(HyCycleViewProvider<HyCyclePageView *> *)provider index:(NSInteger)index {
    NSLog(@"index:%ld",(long)index);
    self.index = index;
    // 主动设置引用
    // provider.retainProvider = YES;
    // __weak typeof(self) weakSelf = self;
    // block 自动引用
    [provider view:^UIView * _Nonnull(HyCyclePageView * _Nonnull cycleView) {
        return self.view;
    }];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate {
    
    !self.scrollViewDidEndDragging ?:
    self.scrollViewDidEndDragging(scrollView);
}
//
//- (void)dealloc
//{
//    NSLog(@"%s", __func__);
//}
@end
